# Maintainer: lauren n. liberda <lauren@selfisekai.rocks>
pkgname=code-oss
pkgver=1.85.1
pkgrel=0
# get this from vscodium
_productjson=49583eb39dd33f265e7a6dec28fe7bd25d418d9c
pkgdesc="Visual Studio Code (OSS, with VSX)"
url="https://github.com/microsoft/vscode"
arch="aarch64 x86_64" # electron
license="MIT"
depends="electron ripgrep"
makedepends="
	electron-dev
	imagemagick
	jq
	krb5-dev
	libsecret-dev
	libxkbfile-dev
	nodejs-dev
	npm
	pngquant
	python3
	yarn
	"
subpackages="
	$pkgname-bash-completion
	$pkgname-zsh-completion
	"
install="$pkgname.post-install"
_l10nd=0.0.30
source="$pkgname-$pkgver.tar.gz::https://github.com/microsoft/vscode/archive/refs/tags/$pkgver.tar.gz
	product-$_productjson.json::https://github.com/VSCodium/vscodium/raw/$_productjson/product.json
	https://github.com/microsoft/vscode-l10n/archive/refs/tags/l10n-dev/v$_l10nd/l10n-dev-$_l10nd.tar.gz
	launcher
	enable-extensions.patch
	no-git.patch
	no-res.patch.noauto
	electron21.patch
	webpack-hash.patch.noauto
	node-sitter.patch.l10n
	no-source-maps.patch
	"
builddir="$srcdir/vscode-$pkgver"
options="!check net" # no tests (that make sense to run..)

export ELECTRON_SKIP_BINARY_DOWNLOAD=1
export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
export npm_config_nodedir=/usr/include/electron/node_headers
export npm_config_build_from_source=true

prepare() {
	default_prepare

	mv -v ../vscode-l10n-l10n-dev-v$_l10nd ../l10n
	msg "Applying more patches"
	for x in $source; do
		case "$x" in
		*.patch.l10n)
			msg "$x"
			patch -p1 -i "$srcdir"/$x -d "$srcdir"/l10n
			;;
		esac
	done

	sed -i '/^disturl\b/d' .yarnrc
	echo 'ignore-engines "true"' >> .yarnrc

	# block husky
	git init .

	(
		cd ../l10n/l10n-dev
		npm ci --ignore-scripts
		npm run compile
		rm -rf node_modules/
	)

	# electron 20+ workaround for bad gyp usage
	# XXX: LARGEFILE64 fixme
	export CFLAGS="$CFLAGS -D_LARGEFILE64_SOURCE"
	export CPPFLAGS="$CPPFLAGS -D_LARGEFILE64_SOURCE"
	export CXXFLAGS="$CXXFLAGS -std=c++17 -D_LARGEFILE64_SOURCE"

	yarn install --cache-folder "$srcdir" --frozen-lockfile

	npm_config_nodedir=/usr npm rebuild tree-sitter tree-sitter-typescript

	patch -Np1 < "$srcdir"/webpack-hash.patch.noauto
	patch -Np1 < "$srcdir"/no-res.patch.noauto

	# patch out telemetry
	# backported from https://github.com/VSCodium/vscodium/blob/master/undo_telemetry.sh
	rg --no-ignore -l "\.data\.microsoft\.com" . \
		| grep -v "\.map\$" | xargs -t -n 1 -P ${JOBS:-2} sed -i -E "s|//[^/]+\.data\.microsoft\.com|//0\.0\.0\.0|g"

	# merge the product.json from the repo with one from vscodium repo
	# this fixes some extensions, including python
	cp product.json original_product.json
	jq -s '.[0] * .[1]' original_product.json "$srcdir"/product-$_productjson.json > product.json
}

build() {
	node --max_old_space_size=4096 ./node_modules/.bin/gulp vscode-linux-x64-min

	cd resources/linux

	mv code.png code-1024-x.png
	local size
	for size in 16 24 32 48 64 128 192 256 512; do
		convert code-1024-x.png -resize ${size}x$size code-$size-x.png
		< code-$size-x.png pngquant --speed 1 - > code-$size.png
	done
	< code-1024-x.png pngquant --speed 1 - > code-1024.png
}

package() {
	mkdir -p "$pkgdir"/usr/share/applications
	sed -e "s|@@NAME_LONG@@|Code - OSS|g" \
		-e "s|@@NAME_SHORT@@|Code - OSS|g" \
		-e "s|@@NAME@@|code-oss|g" \
		-e "s|@@EXEC@@|/usr/bin/code-oss|g" \
		-e "s|@@ICON@@|com.visualstudio.code.oss|g" \
		-e "s|@@URLPROTOCOL@@|code-oss|g" \
		resources/linux/code.desktop > "$pkgdir"/usr/share/applications/code-oss.desktop
	sed -e "s|@@NAME_LONG@@|Code - OSS|g" \
		-e "s|@@NAME_SHORT@@|Code - OSS|g" \
		-e "s|@@NAME@@|code-oss|g" \
		-e "s|@@EXEC@@|/usr/bin/code-oss|g" \
		-e "s|@@ICON@@|com.visualstudio.code.oss|g" \
		-e "s|@@URLPROTOCOL@@|code-oss|g" \
		resources/linux/code-url-handler.desktop > "$pkgdir"/usr/share/applications/code-oss-url-handler.desktop

	for size in 16 24 32 48 64 128 192 256 512 1024; do
		install -Dm644 resources/linux/code-$size.png \
			"$pkgdir"/usr/share/icons/hicolor/${size}x$size/apps/com.visualstudio.code.oss.png
	done

	mkdir -p "$pkgdir"/usr/share/metainfo
	sed -e "s|@@NAME_LONG@@|Code - OSS|g" \
		-e "s|@@NAME@@|code-oss|g" \
		-e "s|@@LICENSE@@|MIT|g" \
		resources/linux/code.appdata.xml > "$pkgdir"/usr/share/metainfo/code-oss.appdata.xml

	mkdir -p "$pkgdir"/usr/share/mime/packages
	sed -e "s|@@NAME_LONG@@|Code - OSS|g" \
		-e "s|@@NAME@@|code-oss|g" \
		resources/linux/code-workspace.xml > "$pkgdir"/usr/share/mime/packages/code-oss-workspace.xml

	mkdir -p "$pkgdir"/usr/share/bash-completion/completions
	sed -e "s|@@APPNAME@@|code-oss|g" \
		resources/completions/bash/code > "$pkgdir"/usr/share/bash-completion/completions/code-oss

	mkdir -p "$pkgdir"/usr/share/zsh/site-functions
	sed -e "s|@@APPNAME@@|code-oss|g" \
		resources/completions/zsh/_code > "$pkgdir"/usr/share/zsh/site-functions/_code-oss

	mkdir -p "$pkgdir"/usr/lib/code-oss/resources
	cp -a ../VSCode-linux-x64/resources/app "$pkgdir"/usr/lib/code-oss/resources/

	# disable update server
	sed -i "/updateUrl/d" "$pkgdir"/usr/lib/code-oss/resources/app/product.json

	# link to system rg
	ln -sfv /usr/bin/rg \
		"$pkgdir"/usr/lib/code-oss/resources/app/node_modules.asar.unpacked/@vscode/ripgrep/bin/rg

	install -Dm755 "$srcdir"/launcher "$pkgdir"/usr/bin/code-oss
}

sha512sums="
f293f5fb74d4f146776a83ad01c8650ecd9f2374977c0adfde794e507254737af68308c39e86c08206800fe25a3f80456db7822581d375053eb459ab08f0b996  code-oss-1.85.1.tar.gz
a811b37deab287dde57574931496d0534c7dccb1d94a13bc371ac91941f3026bbee2604715f98486149163f9ac21aca13cabf4119d32c54603f88179fd38daa9  product-49583eb39dd33f265e7a6dec28fe7bd25d418d9c.json
ff3f1b221c320b9f46cd36ab9a9da79e873b2c9005c069b4cccc85beb283d2d5141de48acc71a95c9103d03d6f9f10bc3acc8b1d5a3494e611f2dfb2fafaf3c2  l10n-dev-0.0.30.tar.gz
0db712e99f7f9a263a8c4222e8c21bbd2ac130c216d752071e9961aba1b98831007e0b9602a375df04dd1c0de805f956c6506d65e5500df855087db614cad08c  launcher
9f36c7fa6f0fd6a516f8e22c47f53013337985e59085bf1ea70165d42a513a92aa2a7a99ef0715e752c7190fd39ba703d405838e61bcfb60cabe47c421781eb4  enable-extensions.patch
4f10b14aaab71e69652662dea0dd5ba00d996282bc864743210185d93a9b7c54c71942fe1e7cd630abd914e3c770eca6c2110c13fb6a8c5e728c5172d1d0cd9f  no-git.patch
48e46bd2ec1490e456a9227c4d74eae5e5c6a7e48a98f8f463370bf60eb7fcb140337e3e02577dea1eabbc48174e2e3b31a4717a945d5dc21ccdbfd3a9807349  no-res.patch.noauto
7effe899c82fb484db686f57b2f80811018ec76f98bc7c4ca6b5edbf651e56e61d565024bf3cd948b54a255c91ec3f1464ee9d9a0e06e966a8232e41bc2de514  electron21.patch
0939ed0e39883b27ac13cdde2e1dbce506043997b6b52610d1f75560db58bac646d6f8fd6b909e8ad595aab6ed9f3777206e33d7fa5a58e0a7acc4c530b5d6b0  webpack-hash.patch.noauto
cc9bd5b2c1f79bf05be3e963803389f935e4ab516d5a3f37dd0a96629561edd7ca179dcfc77b2da3add9ebe072f0374ef25ba00c6b17266615b7019e58c243e8  node-sitter.patch.l10n
20310187474b13bb0d4a7d39cdd93cdb005bf241110da0b02c4fa0927363fe8ea566dd259ce767ceffab771806f3365c209e401f1a3435361582bbedb525c5c7  no-source-maps.patch
"
